# coding: utf8
__author__ = 'Tony'

from umeng_tool.analyse.utils import DatetimeUtils
import random
from umeng_tool.analyse.models import VirtualUser
from umeng_tool.analyse.data_clear import activites_data
import logging


run_log = logging.getLogger('run')


class Task(object):
	"""
	用户登录模拟
	"""
	def __init__(self):
		"""
		:param deep: 被管理的用户深度（天数）
		"""
		self.user_list = []

	def runner(self):
		"""
		任务执行者
		"""
		now = DatetimeUtils.now_utc()
		# self.user_list = self.merge_user_list(
		#     self.user_list,
		#     VirtualUser.objects.filter(launch_datetime__gte=now)
		# )

		users = VirtualUser.objects.filter(launch_datetime__lte=now)

		# for user in self.user_list:
		#     ratio = random.randint(1, 100)
		#     user.launch(
		#         activities=config.ACTIVITIES,
		#         update_session=(ratio > 80)
		#     )
		#     user.make_next_online()
		for user in users:
			ratio = random.randint(1, 100)
			try:
				user.launch(
					activities=activites_data.DataSet(),
					update_session=(ratio > 90)
				)
			except:
				run_log.exception('user_login_task')
			user.make_next_online()

		run_log.info('%s user login' % len(users))


task = Task()

